package com.quanyan.financialSystem.web.place;

import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.financialSystem.entity.vo.req.ReqCashFinishDetailButton;
import com.quanyan.financialSystem.entity.vo.req.ReqCashFinishFinancial;
import com.quanyan.financialSystem.entity.vo.req.ReqCashFinishFinancialDetail;
import com.quanyan.financialSystem.entity.vo.req.ReqPlaceProfitReport;
import com.quanyan.financialSystem.entity.vo.resp.RespCashFinishFinancial;
import com.quanyan.financialSystem.entity.vo.resp.RespCashFinishFinancialDetail;
import com.quanyan.financialSystem.entity.vo.resp.RespPlaceRentCost;
import com.quanyan.financialSystem.service.place.CashFinishOrderFinancialService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xian-guo
 * @email 498152332@qq.com
 * @date 2016/10/22.
 */
@RestController
@RequestMapping(value = "/admin/api/financial/place")
public class CashFinishOrderFinancialController {

    private Logger logger = LoggerFactory.getLogger(CashFinishOrderFinancialController.class);

    @Autowired
    private CashFinishOrderFinancialService cashFinishOrderFinancialService;

    /**
     * 现金结算单
     * @param reqCashFinishFinancial
     * @return
     */
    @RequestMapping(value = "getCashFinishOrder", method = RequestMethod.POST)
    public APIResponse<PageObj<List<RespCashFinishFinancial>>> getCashFinishOrder(@RequestBody ReqCashFinishFinancial reqCashFinishFinancial){
        logger.debug("getCashFinishOrder--------->>>>"+ JSON.toJSONString(reqCashFinishFinancial));
        Map<String,Object> paramMap=new HashMap<>();
        int cityId=reqCashFinishFinancial.getCityId();
        if (cityId!=0){//标示有搜索城市条件
            paramMap.put("cityId",cityId);
        }
        String typeFor=reqCashFinishFinancial.getTypeFor();
        if (!StringUtil.isBlankOrNull(typeFor)){
            paramMap.put("placeIdOrName",typeFor);//搜索场馆编号或名称
        }
        String dateStart=reqCashFinishFinancial.getOrderStartTime();
        if (!StringUtil.isBlankOrNull(dateStart)){//结算日期
            paramMap.put("settleStartTime",dateStart);
        }
        String dateEnd=reqCashFinishFinancial.getOrderEndTime();
        if (!StringUtil.isBlankOrNull(dateEnd)){
            paramMap.put("settleEndTime",dateEnd);
        }
        int status=reqCashFinishFinancial.getStatus();
        if (status!=0){//结算状态
            paramMap.put("statementStatus",status);
        }
        String startDate=reqCashFinishFinancial.getStartDate();
        if (!StringUtil.isBlankOrNull(startDate)){
            paramMap.put("startTime",startDate);
        }
        String endDate=reqCashFinishFinancial.getEndDate();
        if (!StringUtil.isBlankOrNull(startDate)){
            paramMap.put("endTime",endDate);
        }
        paramMap.put("pageNum",reqCashFinishFinancial.getPageNum() * reqCashFinishFinancial.getPageSize());
        paramMap.put("pageSize",reqCashFinishFinancial.getPageSize());
        int impor=reqCashFinishFinancial.getImpor();
        if(impor==1){//导出excel表格
            paramMap.put("impor",impor);
        }
        return cashFinishOrderFinancialService.getCashFinishOrder(paramMap);
    }

    /**
     * 现金结算单明细
     * @param reqCashFinishFinancialDetail
     * @return
     */
    @RequestMapping(value = "getCashFinishOrderDetail", method = RequestMethod.POST)
    public APIResponse<RespCashFinishFinancialDetail> getCashFinishOrderDetail(@RequestBody ReqCashFinishFinancialDetail reqCashFinishFinancialDetail){
        logger.debug("getCashFinishOrderDetail--------->>>>"+ JSON.toJSONString(reqCashFinishFinancialDetail));
        Map<String,Object> paramMap=new HashMap<>();
        String statementId=reqCashFinishFinancialDetail.getStatementId();
        if (StringUtil.isBlankOrNull(statementId)){
            return APIResponse.returnFail("参数不能为空");
        }
        paramMap.put("statementId",statementId);
        paramMap.put("pageNum",reqCashFinishFinancialDetail.getPageNum() * reqCashFinishFinancialDetail.getPageSize());
        paramMap.put("pageSize",reqCashFinishFinancialDetail.getPageSize());
        return cashFinishOrderFinancialService.getCashFinishOrderDetail(paramMap);
    }

    /**
     * 保存按钮
     * 确认结算
     * @param reqCashFinishDetailButton
     * @return
     */
    @RequestMapping(value = "saveCashFinishOrderDetailButton", method = RequestMethod.POST)
    public APIResponse<RespCashFinishFinancialDetail> saveCashFinishOrderDetailButton(@RequestBody ReqCashFinishDetailButton reqCashFinishDetailButton){
        logger.debug("saveCashFinishOrderDetailButton--------->>>>"+ JSON.toJSONString(reqCashFinishDetailButton));
        Map<String,Object> paramMap=new HashMap<>();
        String statementId=reqCashFinishDetailButton.getStatementId();
        String remark=reqCashFinishDetailButton.getSettleRemark();//备注
        if (StringUtil.isBlankOrNull(statementId)){
            return APIResponse.returnFail("参数不能为空");
        }
        int buttonId=reqCashFinishDetailButton.getButtonId();//按钮类型
        paramMap.put("statementId",statementId);
        paramMap.put("remark",remark);
        paramMap.put("buttonId",buttonId);
        if (buttonId==1){//保存备注
            return cashFinishOrderFinancialService.saveCashFinishRemarkButton(paramMap);
        }else if(buttonId==2){//确认结算
            String personName=reqCashFinishDetailButton.getPersonName();
            if (StringUtil.isBlankOrNull(personName)){
                return APIResponse.returnFail("结算人不能为空");
            }
            paramMap.put("personName",personName);
            return cashFinishOrderFinancialService.confirmCashFinishRemarkButton(paramMap);
        }else{
            return APIResponse.returnFail("参数不能为空");
        }
    }

}
